package com.xuan;
 
 
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
 
import java.util.ArrayList;
 
 
/**
 * @author leiliang
 * @date 2021-3-22
 * @email 代码生成器
 */
 
public class Code {
    /**
     * 项目绝对路径
     */
    public static final String PATH = "E:\\project\\springboot+shiro+Jwt";
    /**
     * 表名字,可能有多个例如{"admin","t_admin_role"}
     */
    public static final String[] TABLENAME = {"co_company"};

    public static void main(String[] args) {
        //构建一个代码自动给生成器对象
        AutoGenerator mpg = new AutoGenerator();
        //1、全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(PATH);  //设置项目生成路径 gc.setOutputDir("E://code");
        gc.setAuthor("renzhengxuan");    //设置作者姓名
        gc.setOpen(false);
        gc.setFileOverride(false);  //是否覆盖
        gc.setServiceName("%sService");  //去除service的I前缀（如果不设置service前面将会多一个I）
        gc.setIdType(IdType.ID_WORKER);
        gc.setDateType(DateType.ONLY_DATE);
        gc.setSwagger2(true);
        //xml文件相关
        gc.setActiveRecord(false);// 不需要ActiveRecord特性的请改为false
        gc.setEnableCache(false);// XML 二级缓存
        gc.setBaseResultMap(true);// XML ResultMap
        gc.setBaseColumnList(true);// XML columList
        mpg.setGlobalConfig(gc);
 
        //2、配置数据源
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/xuan?characterEncoding=UTF-8&serverTimezone=GMT%2B8");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);
 
        //3、配置包
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.xuan.modules.system");     //设置生成在哪个父包下
        pc.setEntity("entity");    //设置实体类包名
        pc.setMapper("mapper");
        pc.setService("service");
        pc.setController("controller");
        mpg.setPackageInfo(pc);
 
        //4、策略配置
        StrategyConfig strategy = new StrategyConfig();
        //设置需要生成的表 //设置要映射的表(可包含多个)
        strategy.setInclude(TABLENAME);
        //strategy.setExclude("article");    //也可设置不需要映射的表
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        //strategy.setLogicDeleteFieldName("deleted");   //设置逻辑删除字段
        TableFill createTime = new TableFill("create_time", FieldFill.INSERT);
        TableFill updateTime = new TableFill("update_time", FieldFill.INSERT_UPDATE);
        ArrayList<TableFill> tableFills = new ArrayList<>();
        tableFills.add(createTime);
        tableFills.add(updateTime);
        strategy.setTableFillList(tableFills);
        //乐观锁
        //strategy.setVersionFieldName("version");
        strategy.setRestControllerStyle(true);
        strategy.setControllerMappingHyphenStyle(true);
        mpg.setStrategy(strategy);
 
        //执行
        mpg.execute();
    }
}